<template>
  <div class="prizePool">
    <div class="poolBox">
      <el-form ref="form" :model="poolform" label-width="120px">
        <el-form-item label="活动名称">
          <el-input v-model="poolform.name" clearable placeholder="请输入活动名称" />
        </el-form-item>
        <el-form-item label="优惠券名称">
          <el-input v-model="poolform.couponName" clearable placeholder="请输入活动名称" />
        </el-form-item>
        <el-form-item label="活动时间">
          <el-date-picker
            v-model="poolform.time"
            type="datetimerange"
            start-placeholder="活动开始日期"
            end-placeholder="活动结束日期"
            value-format="timestamp"
            :default-time="['00:00:00', '23:59:59']"
            @change="handleChangtime"
          />
        </el-form-item>
        <el-form-item label="优惠券有效时间">
          <el-date-picker
            v-model="poolform.effectiveTime"
            type="datetimerange"
            start-placeholder="优惠券开始日期"
            end-placeholder="优惠券结束日期"
            value-format="timestamp"
            :default-time="['00:00:00', '23:59:59']"
          />
        </el-form-item>
        <el-form-item label="活动金额">
          <el-input v-model="poolform.activityAmount" clearable placeholder="请输入活动名称" />
        </el-form-item>
        <el-form-item label="优惠券">
          <div v-for="(item, index) in poolform.bonus" :key="index">
            <label>金额</label><el-input v-model="item.couponMoney" clearable placeholder="请输入" style="width:120px;margin-left:10px;" />&nbsp;&nbsp;&nbsp;
            <label>概率</label><el-input v-model="item.probability" clearable placeholder="请输入" style="width:120px;margin-left:10px;" /><p />
          </div>
        </el-form-item>
        <el-form-item label="选择机构及课程">
          <el-radio v-model="poolform.couponUsingRange" label="0">全部机构和课程</el-radio>
          <el-radio v-model="poolform.couponUsingRange" label="1">部分机构和课程</el-radio>
          <el-button v-show="poolform.couponUsingRange === '1'" type="text" style="position:relative;left: -20px;" @click="chooseOrgandCourse">修改</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="alreadyChooseCourse" border style="width: 100%">
        <el-table-column prop="businessName" label="所属机构" align="center" />
        <el-table-column prop="name" label="课程名字" />
        <el-table-column prop="date" label="操作" align="center">
          <template slot-scope="{$index}">
            <el-button type="danger" size="small" @click="handleDelete($index)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="submit">
        <el-button type="primary" class="submit" @click="submit">确定</el-button>
      </div>
    </div>

    <el-dialog title="提示" :visible.sync="dialogVisible" width="1000px">
      <div class="courseListHeader">
        <el-select v-model="bussinessId" filterable placeholder="请选择" @change="selectBussiness">
          <el-option v-for="item in bussinessList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
        <el-input v-model="courseListParams.keyword" clearable placeholder="请输入课程名称" />
        <el-button type="primary" @click="getMerchantCourseList">查 询</el-button>
      </div>
      <br>
      <div class="courseList">
        <el-table ref="multipleTable" :data="courseListData" tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" />
          <el-table-column label="课程封面" width="120">
            <template slot-scope="{row}">
              <img :src="row.introducePictures" class="courseImg">
            </template>
          </el-table-column>
          <el-table-column prop="name" label="姓名" width="120" />
          <el-table-column prop="address" label="地址" show-overflow-tooltip />
        </el-table>
        <pagination
          v-show="courseTotal>0"
          :total="courseTotal"
          :page.sync="courseListParams.currentPage"
          :limit.sync="courseListParams.pageSize"
          @pagination="getMerchantCourseList"
        />
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { prizePool88Update, prizePool88Detail } from '@/api/prizePool'
import { courseBusinessList } from '@/api/course'
import { merchantCourseList } from '@/api/account'
import Pagination from '@/components/Pagination'
export default {
  components: { Pagination },
  data() {
    return {
      poolform: {
        name: '',
        couponName: '',
        time: [],
        effectiveTime: [],
        activityAmount: '',
        couponUsingRange: '',
        bonus: [
          { couponMoney: '', probability: '' },
          { couponMoney: '', probability: '' },
          { couponMoney: '', probability: '' },
          { couponMoney: '', probability: '' },
          { couponMoney: '', probability: '' },
          { couponMoney: '', probability: '' },
          { couponMoney: '', probability: '' },
          { couponMoney: '', probability: '' }
        ]
      },
      dialogVisible: false,
      bussinessList: [],
      bussinessId: '',
      courseListData: [],
      courseListParams: {
        id: '',
        currentPage: 1,
        pageSize: 10,
        keyword: ''
      },
      courseTotal: 0,
      alreadyChooseCourse: [],
      handleArray: [],
      courseIds: '',
      courseNames: '',
      businessIds: '',
      businessNames: '',
      id: ''
    }
  },
  mounted() {
    this.id = this.$route.params['id']
    this.getPrizePoolDetail(this.$route.params['id'])
    this.getBussinessList()
  },
  methods: {
    handleChangtime() {
      console.log(this.poolform.time)
    },
    getPrizePoolDetail(id) {
      prizePool88Detail({ id }).then(res => {
        const { data } = res
        this.poolform.name = data.name || ''
        this.poolform.couponName = data.couponName
        this.poolform.time.push(data.startTime)
        this.poolform.time.push(data.endTime)
        this.poolform.effectiveTime.push(data.beginEffectiveTime)
        this.poolform.effectiveTime.push(data.endEffectiveTime)
        this.poolform.couponUsingRange = '1'
        this.poolform.activityAmount = data.activityAmount
        this.poolform.bonus = data.pools
        this.courseIds = data.courseIds.replace(/\#/g, '').split(',')
        this.courseNames = data.courseNames.split(',')
        this.businessIds = data.businessIds.split(',')
        this.businessNames = data.businessNames.split(',')
        this.courseIds.forEach((item, index) => {
          this.alreadyChooseCourse.push({
            id: item,
            name: this.courseNames[index],
            businessId: this.businessIds[index],
            businessName: this.businessNames[index]
          })
        })
      })
    },
    chooseOrgandCourse() {
      this.dialogVisible = true
    },
    handleSelectionChange(val) {
      if (val.length > 0 && this.alreadyChooseCourse.length > 0) {
        val.forEach(item => {
          this.alreadyChooseCourse.forEach((item2, index2) => {
            if (item.id === item2.id) {
              this.alreadyChooseCourse.splice(index2, 1)
            }
          })
        })
      }
      this.handleArray = val
      this.alreadyChooseCourse = this.alreadyChooseCourse.concat(this.handleArray)
      this.alreadyChooseCourse = [...new Set(this.alreadyChooseCourse)]
    },
    // 获取全部认证机构
    getBussinessList() {
      courseBusinessList().then(response => {
        this.bussinessList = response.data
      })
    },
    // 选择机构
    selectBussiness() {
      this.courseListData = []
      this.courseListParams.id = this.bussinessId
      this.getMerchantCourseList()
    },
    // 查询机构的课程
    getMerchantCourseList() {
      merchantCourseList(this.courseListParams).then(res => {
        const { records, total } = res.data
        this.courseTotal = total
        this.courseListData = records
      })
    },
    handleDelete(index) {
      console.log(this.alreadyChooseCourse)
      this.alreadyChooseCourse.splice(index, 1)
    },
    // 提交保存
    submit() {
      const ids = []; const names = []; const courseIds = []; const courseNames = []
      console.log(this.alreadyChooseCourse)
      this.alreadyChooseCourse.forEach((item, index) => {
        ids.push('#' + item.businessId + '#')
        names.push(item.businessName)
        courseIds.push('#' + item.id + '#')
        courseNames.push(item.name)
      })
      const params = {
        id: this.id,
        name: this.poolform.name,
        couponName: this.poolform.couponName,
        startTime: this.poolform.time[0],
        endTime: this.poolform.time[1],
        beginEffectiveTime: this.poolform.effectiveTime[0],
        endEffectiveTime: this.poolform.effectiveTime[1],
        activityAmount: this.poolform.activityAmount,
        surplusAmount: this.poolform.activityAmount,
        CouponWay: 4,
        businessIds: ids.join(','),
        businessNames: names.join(','),
        courseIds: courseIds.join(','),
        courseNames: courseNames.join(','),
        bonus: JSON.stringify(this.poolform.bonus)
      }
      prizePool88Update(params).then(res => {
        if (res.code === '200') {
          this.$router.go(-1)
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.prizePool{
  padding: 20px 20px 50px 20px;
}
.poolBox{
  .el-input{
    width: 400px;
  }
}
.el-dialog__footer{
  text-align: left!important;
}
.courseList{
  .el-input{
    width: 400px;
  }
}
.courseImg{
  width: 80px;
  height: 80px;
  object-fit: contain;
}
.courseListHeader{
  display: flex;
  align-items: center;
  .el-input{
    width: 300px;
    margin-left: 15px;
  }
  .el-button{
    margin-left: 15px;
  }
}
.submit{
  margin-top: 30px;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
